package com.aigou.admin.dao.impl;
import com.aigou.admin.utils.JDBCUtils;
import com.aigou.admin.dao.Product_typeDao;
import com.aigou.admin.entity.Product_type;
import org.springframework.stereotype.Repository;

import java.util.List;
/**
 * @Author: May
 * @Date: 2024/12/3 12:58
 * @Version: 1.0
 */

@Repository
public class Product_typeDaoImpl implements Product_typeDao {
    @Override
    public List<Product_type> selectAll() {
        return JDBCUtils.executeQuery(Product_type.class, "select * from s_product_type");
    }

    @Override
    public Product_type selectById(String id) {
        List<Product_type> users = JDBCUtils.executeQuery(Product_type.class, "select * from s_product_type where id = ? ",  id);
        if (users.isEmpty()) {
            return null;
        }
        return users.get(0);
    }

    @Override
    public Boolean insert(Product_type productType) {
        int rows = JDBCUtils.executeUpdate("insert into s_product_type(" +
                        "id," +
                        "product_type_name," +
                        "product_type_desc," +
                        "product_type_icon" +
                        ") values(?,?,?,?)",
                productType.getId(),
                productType.getProduct_type_name(),
                productType.getProduct_type_desc(),
                productType.getProduct_type_icon()
        );
        return rows > 0;
    }

    @Override
    public Boolean update(Product_type productType) {
        int rows = JDBCUtils.executeUpdate("update s_product_type set " +
                        "product_type_name=?," +
                        "product_type_desc=?," +
                        "product_type_icon=?" +
                        "where id = ?",
                productType.getProduct_type_name(),
                productType.getProduct_type_desc(),
                productType.getProduct_type_icon(),
                productType.getId()
        );
        return rows > 0;
    }

    @Override
    public Boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_product_type where id = ?", id);
        return rows >0;
    }

    @Override
    public Integer selectUserCount() {
        return JDBCUtils.executeCount("select count(*) from s_product_type");
    }

    @Override
    public List<Product_type> selectUserLimit(int start, int rows) {
        return JDBCUtils.executeQuery(Product_type.class,"select * from s_product_type limit ?,?",start,rows);
    }

    @Override
    public List<Product_type> findByNameContaining(String name) {
        return JDBCUtils.executeQuery(Product_type.class, "select * from s_product_type where product_type_name like ?", "%" + name + "%");
    }
}

